/**
 * Anderground project - Subway maps processor application.
 * Copyright (C) 2008-2009 Eldar Abusalimov, Ilya Shurigyn
 * 
 * This file is part of Anderground.
 * 
 * Anderground is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * Anderground is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Anderground.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.anderground.modules.drawer.dynamic;

import static org.anderground.modules.drawer.dynamic.Config.hubBackRadius;
import static org.anderground.modules.drawer.dynamic.Config.hubRadius;
import static org.anderground.modules.drawer.dynamic.Config.stationBackRadius;
import static org.anderground.modules.drawer.dynamic.Config.stationRadius;

import java.util.ArrayList;

import org.anderground.core.drawer.Calculator;
import org.anderground.core.graphics.Canvas;
import org.anderground.core.graphics.Color;
import org.anderground.core.graphics.PointF;
import org.anderground.core.graphics.RadialGradient;
import org.anderground.core.model.Station;

/**
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 * 
 */
class SimpleAmoeba extends Amoeba {

	@SuppressWarnings("unused")
	private String name;

	public SimpleAmoeba(Crossing crossing, Station station, int color,
			PointF coordF) {
		this.name = station.getName();
		this.crossing = crossing;
		this.size = 1;
		this.coordF = coordF;
		this.stations = new Station[] { station };
		this.colors = new int[] { color };
		this.coords = new PointF[] { coordF };
	}

	public SimpleBacillus searchSimpleBacillus(
			SimpleAmoeba neighborSimpleAmoeba) {
		// we expect that the bacillus associated with the primitive neighbor is
		// primitive. to make sure just see at bacillusMap's put method call
		// hierarchy
		SimpleBacillus bacillus = (SimpleBacillus) getBacillus(neighborSimpleAmoeba);
		if (bacillus == null) {
			Road road = this.crossing
					.searchBacillusCrossing(neighborSimpleAmoeba.crossing);
			bacillus = new SimpleBacillus(road, this,
					neighborSimpleAmoeba);
		}
		return bacillus;
	}

	void addNeighborPrimitive(SimpleAmoeba neighborSimpleAmoeba,
			SimpleBacillus simpleBacillus) {
		addNeighbor(neighborSimpleAmoeba, simpleBacillus);
	}

	public Crossing getCrossingAmoeba() {
		return crossing;
	}

	@Override
	protected void applyCalculator(Calculator calculator) {
		calculator.convert(coordF, pointF);
	}

	@Override
	protected void addDrawnAmoebas(ArrayList<Amoeba> drawnAmoebas) {
		drawnAmoebas.add(this);
	}

	@Override
	protected void draw(Canvas canvas) {
		if (parentAmoeba == null) {
			canvas.drawCircle(pointF, stationBackRadius, backSimplePaint);
			simplePaint.setShader(new RadialGradient(pointF.x + 1,
					pointF.y + 1, stationBackRadius, Color.WHITE, colors[0]));
			canvas.drawCircle(pointF, stationRadius, simplePaint);
		} else {
			canvas.drawCircle(pointF, hubBackRadius, backComplexPaint);
			complexPaint.setShader(new RadialGradient(pointF.x + 1,
					pointF.y + 1, hubBackRadius, Color.WHITE, colors[0]));
			canvas.drawCircle(pointF, hubRadius, complexPaint);
		}
		this.drawLabels(canvas);
	}

	@Override
	protected void highlight(Canvas canvas, int highlightColor) {
		if (parentAmoeba == null) {
			lightSimplePaint.setColor(highlightColor);
			canvas.drawPoint(pointF, lightSimplePaint);
		} else {
			lightComplexPaint.setColor(highlightColor);
			canvas.drawPoint(pointF, lightComplexPaint);
		}
		draw(canvas);
	}

}
